//
//  AppDelegate.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/7.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        setWithoutnav()
        
        
        
        return true
    }
    
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let isHandled : Bool = AlibcTradeSDK.sharedInstance().handleOpen(url)
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        //如果百川处理过会返回true
        if isHandled {
            //其他处理逻辑
            print("\((#file as NSString).lastPathComponent):(\(#line))")
        }
        return true
    }
    
    ///ios9 以后方法
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let isHandled : Bool = AlibcTradeSDK.sharedInstance().handleOpen(url)
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        
        //如果百川处理过会返回true
        if isHandled {
            //其他处理逻辑
            
            print("\((#file as NSString).lastPathComponent):(\(#line))")
        }
        return true
    }
}

extension AppDelegate {
    func setUIAliBC() -> Void {
        
        // 百川平台基础SDK初始化，加载并初始化各个业务能力插件
        AlibcTradeSDK.sharedInstance().asyncInit(success: {
            
        }) { (error) in
            print("\((#file as NSString).lastPathComponent):(\(#line))",error?.localizedDescription as Any)
        }

        // 开发阶段打开日志开关，方便排查错误信息
        AlibcTradeSDK.sharedInstance().setDebugLogOpen(false)
        
        // 配置全局的淘客参数
        //如果没有阿里妈妈的淘客账号,setTaokeParams函数需要调用
        let taokeParsma : AlibcTradeTaokeParams = AlibcTradeTaokeParams()
        
        //mm_XXXXX为你自己申请的阿里妈妈淘客pid
        taokeParsma.pid = "mm_97100348_7476080_24834937"
        
        //设置全局的app标识，在电商模块里等同于isv_code
        //没有申请过isv_code的接入方,默认不需要调用该函数
//        AlibcTradeSDK.sharedInstance().setISVCode("264467e5186e9d31f33bf942adab9fda")
        
        // 设置全局配置，是否强制使用h5
//        AlibcTradeSDK.sharedInstance().setIsForceH5(false)
        AlibcTradeSDK.sharedInstance().setIsForceH5(true)
        
    }
}


extension AppDelegate {
    func setNav() -> Void {
        ///启动界面加导航栏
        let nav : UINavigationController = UINavigationController(rootViewController: ViewController())
        
        
        window?.rootViewController = nav
        window?.makeKeyAndVisible()
        
        ///导航栏设置
        UINavigationBar.appearance().tintColor = UIColor.blue
        
        ///阿里百川
        setUIAliBC()
    }
    
    func setWithoutnav() -> Void {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = MainViewVC()
        window?.makeKeyAndVisible()
        
        ///阿里百川
        setUIAliBC()
    }
}
